#pragma once

#include "ActionManager.h"

namespace VSFramework{
	class IVSFramework;
}

class CSession;
class CContentHandler : public IContentAppOperator
{
private:
	CSession*		m_pSession;

public:
	void Create( CSession* session )
	{
		m_pSession = session;
	}

	//handle patch process and file transferring result
	virtual bool IsFileExists( const char* file_name ) { return false; }
	virtual void DeleteOldFiles( const char* file_name ) {}
	virtual void FileDownloadCompleted( const char* file_name, unsigned char* buffer, size_t size, const char cProcessType ) {}
};

//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

class CSession
{
public:
	CSession(void);
	~CSession(void);

	friend class CContentHandler;

	const int GetSessioinID(void) { return m_sessionID; }

private:
	class CSessionGroup*	m_pOwnGroup;
	int					m_sessionID;

	unsigned int		m_uiFirstSecureKey;
	unsigned int		m_uiSecodSecureKey;

	IVSFramework*		m_pVSFramework;
	CContentHandler		m_FrameworkHandler;

private:
	typedef std::function< void(cJSON*) > delegate_msg_handler;

private:
	CActionManager		m_ActionManager;

	typedef std::pair<std::string,std::string> PAIR_STR;
	typedef std::function< std::pair<std::string,std::string>() > request_actor;
	typedef std::map<std::string, request_actor> MAP_REQUESTS;
	MAP_REQUESTS		m_requests;

	typedef void (CSession::*action_handler)(int count);
	typedef std::map<std::string, action_handler> MAP_ACTIONS;
	MAP_ACTIONS	m_actions;

public:
	void	Initialize( CSessionGroup* pGroup, int session_id );
	void	Update();

	void	HandleMsg( const std::string& msg_id, cJSON* packet );
	void	ExecuteAction( int execution_count, const std::string& action_name );
	void	JobFinished();

private:
	void	BindActionHandler();
	void	BindNetworkHandler();

private:
	void	InvokeAction( const request_actor& actor );

	void ActionBehaviorAIMissionProgress(int count) { InvokeAction(m_requests["req_mission_progress"]); }
	void ActionBehaviorAIBattleProgress(int count) { InvokeAction(m_requests["req_battle_progress"]); }
	void ActionBehaviorAIRaidProgress(int count) { InvokeAction(m_requests["req_raid_progress"]); }

	void ActionBehaviorEnterGame(int count) { InvokeAction(m_requests["req_enter_game"]); }

	// login
	void ActionBehaviorReadyToPlay(int count) { InvokeAction(m_requests["req_ready_to_play"]); }
	void ActionBehaviorSetSpecies(int count) { InvokeAction(m_requests["req_set_species"]); }
	void ActionBehaviorSetName(int count) { InvokeAction(m_requests["req_set_name"]); }

	// mission
	void ActionBehaviorEnterMission(int count) { InvokeAction(m_requests["req_enter_mission"]); }
	void ActionBehaviorMissionQuestProgress(int count) { InvokeAction(m_requests["req_quest_progress"]); }

	// battle
	void ActionBehaviorEnterBattle(int count) { InvokeAction(m_requests["req_enter_battle"]); }

	void ActionBehaviorBattleRankList(int count) { InvokeAction(m_requests["req_rank_list"]); }
	void ActionBehaviorBattleRivalList(int count) { InvokeAction(m_requests["req_rival_list"]); }
	void ActionBehaviorBattleCombatStart(int count) { InvokeAction(m_requests["req_combat_start"]); }
	void ActionBehaviorBattleHistoryList(int count) { InvokeAction(m_requests["req_history_list"]); }

	void ActionBehaviorBattleFirstRival(int count) { InvokeAction(m_requests["req_first_rival"]); }
	void ActionBehaviorBattleFirstStart(int count) { InvokeAction(m_requests["req_first_start"]); }
	
	// raid
	void ActionBehaviorEnterRaid(int count) { InvokeAction(m_requests["req_enter_raid"]); }

	void ActionBehaviorRaidStart(int count) { InvokeAction(m_requests["req_start"]); }
	void ActionBehaviorRaidResult(int count) { InvokeAction(m_requests["req_result"]); }

	// enhance
	void ActionBehaviorCardUpgrade(int count) { InvokeAction(m_requests["req_upgrade"]); }
	void ActionBehaviorCardEvolution(int count) { InvokeAction(m_requests["req_evolution"]); }

	// shop
	void ActionBehaviorEnterShop(int count) { InvokeAction(m_requests["req_enter_shop"]); }

	void ActionBehaviorShopDrawCard(int count) { InvokeAction(m_requests["req_draw_card"]); }
	void ActionBehaviorShopItemBuy(int count) { InvokeAction(m_requests["req_buy_item"]); }
	void ActionBehaviorShopCoinBuy(int count) { InvokeAction(m_requests["req_buy_coin"]); }

	// manage
	void ActionBehaviorEnterManage(int count) { InvokeAction(m_requests["req_enter_manage"]); }

	void ActionBehaviorManageChangeMsg(int count) { InvokeAction(m_requests["req_change_msg"]); }
	void ActionBehaviorManageCardList(int count) { InvokeAction(m_requests["req_card_list"]); }
	void ActionBehaviorManageSellCard(int count) { InvokeAction(m_requests["req_sell_card"]); }
	void ActionBehaviorManageItemList(int count) { InvokeAction(m_requests["req_item_list"]); }
	void ActionBehaviorManageUseItem(int count) { InvokeAction(m_requests["req_use_item"]); }
	void ActionBehaviorManageUpdateStat(int count) { InvokeAction(m_requests["req_update_stat"]); }

	// formation
	void ActionBehaviorEnterFormation(int count) { InvokeAction(m_requests["req_enter_formation"]); }

	void ActionBehaviorFormationSet(int count) { InvokeAction(m_requests["req_set"]); }

	// friend
	void ActionBehaviorFriendList(int count) { InvokeAction(m_requests["req_list"]); }
	void ActionBehaviorFriendDelete(int count) { InvokeAction(m_requests["req_delete"]); }

	void ActionBehaviorFriendProposeList(int count) { InvokeAction(m_requests["req_propose_list"]); }
	void ActionBehaviorFriendAccept(int count) { InvokeAction(m_requests["req_accept"]); }
	void ActionBehaviorFriendReject(int count) { InvokeAction(m_requests["req_reject"]); }
	void ActionBehaviorFriendCancel(int count) { InvokeAction(m_requests["req_cancel"]); }

	void ActionBehaviorFriendCandidateList(int count) { InvokeAction(m_requests["req_candidate_list"]); }
	void ActionBehaviorFriendSearch(int count) { InvokeAction(m_requests["req_search"]); }
	void ActionBehaviorFriendRequest(int count) { InvokeAction(m_requests["req_request"]); }

	// post
	void ActionBehaviorEnterPost(int count) { InvokeAction(m_requests["req_enter_post"]); }

	void ActionBehaviorPostList(int count) { InvokeAction(m_requests["req_list"]); }
	void ActionBehaviorPostCheck(int count) { InvokeAction(m_requests["req_check"]); }
	void ActionBehaviorPostGetItem(int count) { InvokeAction(m_requests["req_get_item"]); }
	void ActionBehaviorPostDelete(int count) { InvokeAction(m_requests["req_delete"]); }

	// system
	void ActionBehaviorUserRefreshTime(int count) { InvokeAction(m_requests["req_user_refresh"]); }
		// noti
	void ActionBehaviorNotiLive(int count) { InvokeAction(m_requests["req_noti_live"]); }
	void ActionBehaviorNotiAlert(int count) { InvokeAction(m_requests["req_noti_alert"]); }
		// banner
	void ActionBehaviorFrontBanner(int count) { InvokeAction(m_requests["req_front_banner"]); }

	void ActionBehaviorUserReset(int count) { InvokeAction(m_requests["user_reset"]); }

private:
	void BindMsgHandlers();

	typedef void (CSession::*FRAMEWORKMSG_HANDLER)( unsigned char param );
	typedef std::map<unsigned char,FRAMEWORKMSG_HANDLER> MAP_FRAMEWORK_HANDLERS;
	MAP_FRAMEWORK_HANDLERS	m_mapFrameworkMsgHandlers;
	#define REG_FRAMEWORKMSG_HANDLE(msg_id, handler) m_mapFrameworkMsgHandlers[msg_id]=(FRAMEWORKMSG_HANDLER)&CSession::handler;

	typedef void (CSession::*MSG_HANDLER)( cJSON* packet );
	typedef std::map<std::string,MSG_HANDLER> MAP_HANDLERS;
	MAP_HANDLERS	m_mapMsgHandlers;
	#define REG_MSG_HANDLE(msg_id, handler) m_mapMsgHandlers.insert(MAP_HANDLERS::value_type(msg_id,(MSG_HANDLER)&CSession::handler));

	void OnVS_FORWARDEDMSG_MODULE_CERTIFICATED(unsigned char param);
	void OnVSFRAMEWORK_ACCOUNT_REGISTER_ACK(unsigned char param);
	void OnVSFRAMEWORK_LOGIN_ACK(unsigned char param);
	void OnVSFRAMEWORK_NETWORK_ERROR(unsigned char param);
};

